package ph.com.gs3.formalistics.adapters;

import java.util.List;

import ph.com.gs3.formalistics.R;
import ph.com.gs3.formalistics.interfaces.view.UserActionListItemInterface;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class DocumentListItemViewAdapter extends BaseAdapter {

	private Context context;
	private List<UserActionListItemInterface> list;

	private DocumentListItemActionListener listener;

	public DocumentListItemViewAdapter(Context context,
			List<UserActionListItemInterface> list) {
		this.context = context;
		this.list = list;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	private static class DocumentListViewFragmentHolder {

		// CONTENT COMING FROM JAVA
		TextView header;
		TextView headerInfo;
		TextView headerInfoDataB;
		TextView headerInfoContents;
		TextView footerTitle;
		TextView footerContent;

		public DocumentListViewFragmentHolder(View view) {

			// FIND ID CONTAINER of the DATA
			header = (TextView) view.findViewById(R.id.DL_tvSubHeader);
			headerInfo = (TextView) view.findViewById(R.id.DL_tvheader);
			headerInfoDataB = (TextView) view
					.findViewById(R.id.DL_tvheader_action_date);

			headerInfoContents = (TextView) view
					.findViewById(R.id.DL_tvcontents);
			footerTitle = (TextView) view.findViewById(R.id.DL_tvfooter_title);
			footerContent = (TextView) view
					.findViewById(R.id.DL_tvfooter_content);

		}

	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		View row = convertView;
		DocumentListViewFragmentHolder holder = null;

		if (row == null) {

			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			row = inflater.inflate(R.layout.view_document_list_item, parent,
					false);
			holder = new DocumentListViewFragmentHolder(row);
			row.setTag(holder);

			final ImageView CommentBtn = (ImageView) row
					.findViewById(R.id.DL_ivcomment_icon);
			CommentBtn.setClickable(true);
			// CommentBtn.setOnClickListener(new CommentAction());

			CommentBtn.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					if (listener != null) {
						listener.onClickedComments(position);
					}
				}
			});

		} else {
			holder = (DocumentListViewFragmentHolder) row.getTag();
		}

		UserActionListItemInterface data = list.get(position);

		holder.header.setText(data.getHeader());
		holder.headerInfo.setText(data.getHeaderInfo());
		holder.headerInfoDataB.setText(data.getHeaderInfoDataB());

		holder.headerInfoContents.setText(data.getContent());
		holder.footerTitle.setText(data.getFooterTitle());
		holder.footerContent.setText(data.getFooterContent());

		return row;
	}

	public void registerListener(DocumentListItemActionListener listener) {
		this.listener = listener;
	}

	public static interface DocumentListItemActionListener {

		public void onClickedActions(int position);

		public void onStarredDocument(int position);

		public void onUnstarredDocument(int position);

		public void onClickedComments(int position);

	}

}
